package com.miage.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.miage.bdd.DaoBdd;
import com.miage.bean.Evenement;
import com.miage.utils.Constante;

public class DaoEvenement extends DaoBdd {

	private static final String TABLE = Constante.EVENEMENT_TABLE;

	private static final String ATTR_ID = Constante.EVENEMENT_COLUMN_ID;
	private static final String ATTR_NOM = Constante.EVENEMENT_COLUMN_NOM;
	private static final String ATTR_DATE_DEBUT = Constante.EVENEMENT_COLUMN_DATE_DEBUT;
	private static final String ATTR_DATE_FIN = Constante.EVENEMENT_COLUMN_DATE_FIN;

	public DaoEvenement(Context pContext) {
		super(pContext);
	}
	
	public void ajouter(Evenement e) {
		this.open();
		
		ContentValues value = new ContentValues();
		value.put(ATTR_ID, e.getId());
		value.put(ATTR_NOM, e.getNom());
		value.put(ATTR_DATE_DEBUT, e.getDateDebut());
		value.put(ATTR_DATE_FIN, e.getDateFin());
		bdd.insert(TABLE, null, value);
		
		this.close();
	}
	
	public void supprimer(Integer id) {
		this.open();
		
		bdd.delete(
				TABLE, 
				ATTR_ID + " = ?", 
				new String[] { String.valueOf(id) }
		);
		
		this.close();
	}
	
	public void modifier(Evenement e) {
		this.open();
		
		ContentValues value = new ContentValues();
		value.put(ATTR_NOM, e.getNom());
		value.put(ATTR_DATE_DEBUT, e.getDateDebut());
		value.put(ATTR_DATE_FIN, e.getDateFin());
		
		bdd.update(
				TABLE, 
				value, 
				ATTR_ID + " = ?",
				new String[] { String.valueOf(e.getId()) }
			);
		
		this.close();
	}
	
	public Evenement getEvenementById(Integer id) {
		this.open();
		
		Cursor c = bdd.query(
						TABLE, new String[] { ATTR_ID, ATTR_NOM, ATTR_DATE_DEBUT, ATTR_DATE_FIN },
						ATTR_ID + " = ?", 
						new String[] { String.valueOf(id) }, 
						null,
						null, 
						null, 
						null
					);
		
		if (c != null) {
			c.moveToFirst();
		}
		
		this.close();
		
		return new Evenement(
				c.getInt(c.getColumnIndex(ATTR_ID)), 
				c.getString(c.getColumnIndex(ATTR_NOM)),
				c.getString(c.getColumnIndex(ATTR_DATE_DEBUT)),
				c.getString(c.getColumnIndex(ATTR_DATE_FIN))
			);
	}
	
	public List<Evenement> getEvenements() {
		this.open();
		
		List<Evenement> listEvenements = new ArrayList<Evenement>();
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_NOM, ATTR_DATE_DEBUT, ATTR_DATE_FIN },
				null, 
				null, 
				null,
				null, 
				null, 
				null
			);
		
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			listEvenements.add(
					new Evenement(
							cursor.getInt(cursor.getColumnIndex(ATTR_ID)),
							cursor.getString(cursor.getColumnIndex(ATTR_NOM)),
							cursor.getString(cursor.getColumnIndex(ATTR_DATE_DEBUT)),
							cursor.getString(cursor.getColumnIndex(ATTR_DATE_FIN))
					)
			);
		}
		
		this.close();
		cursor.close();
		return listEvenements;
	}
	
	
	
	/* VERSION 2 ?? 

	public Evenements getEvenements() {
		Evenements events = new Evenements();
		
		List<Evenement> listEvenements = new ArrayList<Evenement>();
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_LIBELLE },
				null, 
				null, 
				null,
				null, 
				null, 
				null
			);
		
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			listEvenements.add(
					new Evenement(
							cursor.getLong(cursor.getColumnIndex(ATTR_ID)),
							cursor.getString(cursor.getColumnIndex(ATTR_LIBELLE))
					)
			);
		}
		cursor.close();
		events.setListeEvt(listEvenements);
		return events;
	}
	
	*/
}
